package com.renchunlin.taobaounion.ui.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.model.domain.IBaseInfo;
import com.renchunlin.taobaounion.model.domain.SelectedContent;
import com.renchunlin.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import static com.renchunlin.taobaounion.utils.Constants.SUCCESS_CODE;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/1/25.
 * PS: Not easy to write code, please indicate.
 */
public class SelectedPageContentAdapter extends RecyclerView.Adapter<SelectedPageContentAdapter.InnerHolder> {

    List<SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData
            = new ArrayList<>();

    private OnSelectedPageContentItemClickListener mOnSelectedPageContentItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_content, parent, false);
        return new InnerHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {

        holder.setData(mData.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSelectedPageContentItemClickListener != null) {
                    mOnSelectedPageContentItemClickListener.onContentItemClick(mData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectedContent content) {
        if (content.getCode() == SUCCESS_CODE) {
            List<SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
            this.mData.clear();
            this.mData.addAll(map_data);
            notifyDataSetChanged();
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        ImageView selected_cover;
        TextView select_off_prise, selected_title,
                selected_buy_btn, selected_original_prise;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            selected_cover = itemView.findViewById(R.id.selected_cover);
            select_off_prise = itemView.findViewById(R.id.select_off_prise);
            selected_title = itemView.findViewById(R.id.selected_title);
            selected_buy_btn = itemView.findViewById(R.id.selected_buy_btn);
            selected_original_prise = itemView.findViewById(R.id.selected_original_prise);
        }

        public void setData(SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
            selected_title.setText(mapDataBean.getTitle());
            String pict_url = UrlUtils.getCoverPath(mapDataBean.getPict_url());
            Glide.with(itemView.getContext()).load(pict_url).into(selected_cover);
            if (TextUtils.isEmpty(mapDataBean.getCoupon_click_url())) {
                selected_original_prise.setText("晚啦，没有优惠券了");
                selected_buy_btn.setVisibility(View.GONE);
            } else {
                selected_original_prise.setText("原价：" + mapDataBean.getZk_final_price());
                selected_buy_btn.setVisibility(View.VISIBLE);
            }

            if (TextUtils.isEmpty(mapDataBean.getCoupon_info())) {
                select_off_prise.setVisibility(View.GONE);
            } else {
                select_off_prise.setVisibility(View.VISIBLE);
                select_off_prise.setText(mapDataBean.getCoupon_info());
            }
        }
    }

    public void setOnSelectedPageContentItemClickListener(OnSelectedPageContentItemClickListener onSelectedPageContentItemClickListener) {
        mOnSelectedPageContentItemClickListener = onSelectedPageContentItemClickListener;
    }

    public interface OnSelectedPageContentItemClickListener {
        void onContentItemClick(IBaseInfo item);
    }
}
